#include"queue.h"

void Queue_Init(Queue* p) {
	assert(p);
	p->head = p->tail = NULL;
}

void Queue_Destory(Queue* p) {
	assert(p);
	Q* tmp = p->head;
	while (tmp) {
		Q* cur = tmp->next;
		free(tmp);
		tmp = cur;
	}
	p->head = p->tail = NULL;
}

void Queue_Push(Queue* p, QDateType x) {
	assert(p);

	Q* tmp = (Q*)malloc(sizeof(Q));
	if (tmp == NULL) {
		printf("Malloc fail\n");
		exit(-1);
	}
	tmp->data = x;
	tmp->next = NULL;

	if (p->head == NULL) {
		p->head = p->tail = tmp;
	}
	else {
		p->tail->next = tmp;
		p->tail = tmp;
	}
}

void Queue_Pop(Queue* p) {
	assert(p);
	assert(p->head);
	if (p->head == p->tail) {
		free(p->head);
		p->head = p->tail = NULL;
	}
	else {
		Q* tmp = p->head->next;
		free(p->head);
		p->head = tmp;
	}
}

QDateType Queue_Front(Queue* p) {
	assert(p);
	assert(p->head);
	return p->head->data;
}
QDateType Queue_Back(Queue* p) {
	assert(p);
	assert(p->tail);
	return p->tail->data;
}
int Queue_Size(Queue* p) {
	assert(p);
	int size = 0;
	Q* tmp = p->head;
	while (tmp) {
		size++;
		tmp = tmp->next;
	}
	return size;
}
bool Queue_Empty(Queue* p) {
	assert(p);
	return p->head == NULL;
}